@echo off
chcp 65001 > nul
echo ===================================================================
echo   One-click Rebuild, Upload, and Deploy code2law Service
echo ===================================================================
echo.

REM ===================================================================
REM ||                  CONFIGURATION VARIABLES                      ||
REM ===================================================================

REM --- Basic Settings ---
set IMAGE_NAME=code2law
set TAR_DIR=d:\temp\tar
set CONTAINER_NAME=code2law
set CONTAINER_PORT=7642
set APP_INTERNAL_PORT=5000

REM --- Remote Server Settings ---
REM !!! IMPORTANT: Please fill in your remote server details here !!!
set REMOTE_USER=gpzx
set REMOTE_HOST=172.29.173.42
set REMOTE_TAR_DIR=/bak/tar

REM --- Advanced: Remote Docker Commands Configuration ---
set DOCKER_STOP_CMD=docker stop %CONTAINER_NAME%
set DOCKER_REMOVE_CMD=docker rm %CONTAINER_NAME%

REM This command cleans up unused Docker images on the remote server to save space.
set DOCKER_PRUNE_CMD=docker image prune -f

REM The full docker run command, based on your original script.
REM Note the inclusion of the --restart unless-stopped policy and data volume mount.
REM Data will be persisted in /d/docker/code2law/data on the remote server.
set DOCKER_RUN_CMD=docker run -d --name %CONTAINER_NAME% -p %CONTAINER_PORT%:%APP_INTERNAL_PORT% -v /d/docker/code2law/data:/app/data --restart unless-stopped %IMAGE_NAME%

REM ===================================================================
REM ||                      SCRIPT LOGIC                             ||
REM ||             (Usually no need to modify below)                 ||
REM ===================================================================

set TAR_FILE=%TAR_DIR%\%IMAGE_NAME:/=__%.tar
set DEPLOY_SCRIPT_LOCAL=deploy_temp.sh
set DEPLOY_SCRIPT_REMOTE=%REMOTE_TAR_DIR%/deploy.sh

echo [Step 1/7] Check and create local temp directory...
if not exist "%TAR_DIR%" (
    mkdir "%TAR_DIR%"
    echo Created directory: %TAR_DIR%
) else (
    echo Directory exists: %TAR_DIR%
)
echo.

echo [Step 2/7] Build Docker image...
docker build -t %IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo ERROR: Docker image build failed!
    pause
    exit /b 1
)
echo Docker image built successfully!
echo.

echo [Step 3/7] Save Docker image to tar file...
echo Saving image to %TAR_FILE%...
docker save -o "%TAR_FILE%" %IMAGE_NAME%
if %errorlevel% neq 0 (
    echo ERROR: Docker image save failed!
    pause
    exit /b 1
)
echo Image saved successfully!
echo.

echo [Step 4/7] Upload image to remote server...
REM Using -F nul to ignore local SSH config files and force use of REMOTE_USER from this script.
scp -F nul "%TAR_FILE%" %REMOTE_USER%@%REMOTE_HOST%:%REMOTE_TAR_DIR%/
if %errorlevel% neq 0 (
    echo ERROR: Image upload failed!
    echo TIP: Make sure OpenSSH client is installed and passwordless SSH is configured.
    pause
    exit /b 1
)
echo Image uploaded successfully!
echo.

echo [Step 5/7] Create deployment script locally...
(
    echo #!/bin/bash
    echo set -e
    echo echo "[Remote] Starting deployment for %CONTAINER_NAME%..."
    echo cd %REMOTE_TAR_DIR%
    echo echo "[Remote] Loading Docker image from tar file..."
    echo docker load -i %IMAGE_NAME:/=__%.tar
    echo echo "[Remote] Stopping and removing old container..."
    echo %DOCKER_STOP_CMD% 2^>^&1 ^|^| true
    echo %DOCKER_REMOVE_CMD% 2^>^&1 ^|^| true
    echo echo "[Remote] Pruning old images..."
    echo %DOCKER_PRUNE_CMD% 2^>^&1 ^|^| true
    echo echo "[Remote] Starting new container with configured command..."
    echo %DOCKER_RUN_CMD%
    echo echo "[Remote] Deployment completed!"
    echo docker ps ^| grep %CONTAINER_NAME%
) > "%DEPLOY_SCRIPT_LOCAL%"
echo Deployment script "%DEPLOY_SCRIPT_LOCAL%" created successfully.
echo.

echo [Step 6/7] Upload deployment script to remote server...
REM Using -F nul to ignore local SSH config files and force use of REMOTE_USER from this script.
scp -F nul "%DEPLOY_SCRIPT_LOCAL%" %REMOTE_USER%@%REMOTE_HOST%:%DEPLOY_SCRIPT_REMOTE%
if %errorlevel% neq 0 (
    echo ERROR: Failed to upload deployment script!
    pause
    exit /b 1
)
echo Deployment script uploaded successfully!
echo.

echo [Step 7/7] Execute deployment script on remote server...
REM Using -F nul to ignore local SSH config files and force use of REMOTE_USER from this script.
ssh -F nul %REMOTE_USER%@%REMOTE_HOST% "dos2unix %DEPLOY_SCRIPT_REMOTE% && chmod +x %DEPLOY_SCRIPT_REMOTE% && %DEPLOY_SCRIPT_REMOTE%"
if %errorlevel% neq 0 (
    echo ERROR: Remote deployment execution failed!
    echo TIP: Ensure 'dos2unix' is installed on the remote server (sudo apt-get install dos2unix).
    pause
    exit /b 1
)
echo Remote deployment executed successfully!
echo.

REM Cleanup local temporary script
if exist "%DEPLOY_SCRIPT_LOCAL%" (
    del "%DEPLOY_SCRIPT_LOCAL%"
)

echo ===================================================================
echo                        ALL DONE!
echo ===================================================================
echo.
echo  Access your application at: http://%REMOTE_HOST%:%CONTAINER_PORT%
echo  Access admin panel at:    http://%REMOTE_HOST%:%CONTAINER_PORT%/admin
echo.
echo  To view logs on server, run:
echo    ssh -F nul %REMOTE_USER%@%REMOTE_HOST% "docker logs %CONTAINER_NAME%"
echo.
echo  To check status on server, run:
echo    ssh -F nul %REMOTE_USER%@%REMOTE_HOST% "docker ps -a"
echo.
pause